﻿using System.Net;

namespace ParallelLib.Micro
{
    /// <summary>
    /// 节点信息
    /// </summary>
    public class NodeKey
    {
        public NodeKey(string name, int port)
        {
            Name = name;
            Port = port;
            Address = $"tcp://{name}:{port}";
            HostName = Dns.GetHostEntry(name).HostName;
        }

        public NodeKey(string name, int port, string id)
        {
            Name = name;
            Port = port;
            Address = $"tcp://{name}:{port}";
            HostName = Dns.GetHostEntry(name).HostName;
            ID = id;
        }

        public string Address { get; }
        public string HostName { get; private set; }
        public string ID { get; set; }
        public string Name { get; }
        public int Port { get; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((NodeKey)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Name?.GetHashCode() ?? 0) * 397) ^ Port;
            }
        }

        public override string ToString()
        {
            return Address;
        }

        public string ToStringEx()
        {
            return string.Format("{0}\t{1}", Address, ID);
        }

        protected bool Equals(NodeKey other)
        {
            return string.Equals(Name, other.Name)
                && Port == other.Port
                && ID == other.ID;
        }
    }
}